package com.ktjy.ktmall.servlet;

import com.alibaba.fastjson.JSONArray;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.entity.CategoryItem;
import com.ktjy.ktmall.entity.GoodsItem;
import com.ktjy.ktmall.entity.Page;
import com.ktjy.ktmall.service.GoodsItemService;
import com.ktjy.ktmall.service.impl.GoodsItemServiceImpl;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

// 处理来自小程序端商品模块的请求
@WebServlet(name = "GoodsItemServlet", urlPatterns = "/uni/home/goods/guessLike")
public class GoodsItemServlet extends HttpServlet {

    GoodsItemService service = null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        service = new GoodsItemServiceImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取opr请求参数
        String opr = request.getParameter("opr");
        AjaxResult result = null;
        if ("list".equals(opr)) {
            // 3、接收从小程序传递过来的当前页码currPageNo和每页显示的数据行数pageSize
            Integer currPageNo = 1;  // 当前页码
            if (request.getParameter("currPageNo") != null) {
                currPageNo = Integer.valueOf(request.getParameter("currPageNo"));
            }
            Integer pageSize = 6;  // 每页显示的数据行数
            if (request.getParameter("pageSize") != null) {
                pageSize = Integer.valueOf(request.getParameter("pageSize"));
            }
            // 4、调用serivce中查询总记录数的方法
            int count = service.getCount();
            // 5、调用service中查询商品的分页列表数据
            List<GoodsItem> list = service.getGoodsListPage(currPageNo, pageSize);
            // 6、封装Page对象
            Page page = new Page();
            page.setCurrPageNo(currPageNo);
            page.setPageSize(pageSize);
            page.setTotalCount(count);
            page.setList(list); // 将查询到的商品分页列表数据进行赋值

            result = AjaxResult.success("猜你喜欢的商品列表加载成功!", page);
        } else if ("findById".equals(opr)) {
            // 根据商品id加载商品详情信息
            if (!StringUtils.isNullOrEmpty(request.getParameter("id"))) {
                // 1、获取从小程序端传递过来的id参数，参数代表商品id
                Integer id = Integer.valueOf(request.getParameter("id"));
                // 2、调用service中的方法实现根据商品id加载商品详情
                GoodsItem goods = service.findById(id);
                result = AjaxResult.success("商品详情加载成功!", goods);
            } else {
                result = AjaxResult.error("商品id不能为空!");
            }
        }
        // 将result对象转换成JSON字符串再响应到小程序端
        out.print(JSONArray.toJSONString(result));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
